home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / xmesaP.h < prev   
Encoding:
C/C++ Source or Header  |  1997-03-13  |  12.4 KB  |  441 lines

  1. /* $Id: xmesaP.h,v 1.10 1997/01/31 23:44:24 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.2
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: xmesaP.h,v $
  26.  * Revision 1.10  1997/01/31 23:44:24  brianp
  27.  * added FLAT_DITHER macros inspired by code from Martin Schenk (schenkm@ping.at)
  28.  *
  29.  * Revision 1.9  1997/01/31 20:41:20  brianp
  30.  * added vishandle to xmesa_visual struct
  31.  *
  32.  * Revision 1.8  1997/01/08 20:54:02  brianp
  33.  * added DITHER666 option from Michael Pichler
  34.  *
  35.  * Revision 1.7  1996/10/22 02:54:35  brianp
  36.  * incorporated Jacques Leroy's changes to DITHER_HPCR macro
  37.  *
  38.  * Revision 1.6  1996/10/22 02:50:46  brianp
  39.  * new DITHER_SETUP, XDITHER_SETUP and XDITHER macros
  40.  * _MIX macro now uses shifts and ORs instead of mults and adds
  41.  *
  42.  * Revision 1.5  1996/09/27 17:10:20  brianp
  43.  * added index_bits field to XMesaVisual struct
  44.  *
  45.  * Revision 1.4  1996/09/27 01:32:12  brianp
  46.  * changed kernel1 array from 2-D to 1-D
  47.  *
  48.  * Revision 1.3  1996/09/19 03:16:04  brianp
  49.  * new X/Mesa interface with XMesaContext, XMesaVisual, and XMesaBuffer types
  50.  *
  51.  * Revision 1.2  1996/09/15 14:22:13  brianp
  52.  * now use GLframebuffer and GLvisual
  53.  *
  54.  * Revision 1.1  1996/09/13 01:38:16  brianp
  55.  * Initial revision
  56.  *
  57.  */
  58.  
  59.  
  60. #ifndef XMESAP_H
  61. #define XMESAP_H
  62.  
  63.  
  64. #ifdef SHM
  65. #  include <X11/extensions/XShm.h>
  66. #endif
  67. #include "GL/xmesa.h"
  68. #include "types.h"
  69.  
  70.  
  71.  
  72. /*
  73.  * Mesa wrapper for XVisualInfo
  74.  */
  75. struct xmesa_visual {
  76.     GLvisual *gl_visual;    /* Device independent visual parameters */
  77.     Display *display;    /* The X11 display */
  78.     XVisualInfo *vishandle;    /* The pointer returned by glXChooseVisual */
  79.     XVisualInfo *visinfo;    /* Private copy of vishandle's struct */
  80.  
  81.     GLint level;        /* 0=normal, 1=overlay, etc */
  82.  
  83.     GLboolean ximage_flag;    /* Use XImage for back buffer (not pixmap)? */
  84.  
  85.         GLuint dithered_pf;    /* Pixel format when dithering */
  86.         GLuint undithered_pf;    /* Pixel format when not dithering */
  87.  
  88.         GLfloat RedGamma;    /* Gamma values, 1.0 is default */
  89.         GLfloat GreenGamma;
  90.         GLfloat BlueGamma;
  91.  
  92.         GLint rmult, gmult, bmult;    /* Range of color values */
  93.         GLint index_bits;        /* Bits per pixel in CI mode */
  94.  
  95.     /* For PF_TRUECOLOR */
  96.     GLint rshift, gshift, bshift;    /* Bit shifts */
  97.     unsigned long r_to_pixel[256];    /* Converts red to pixel bits */
  98.     unsigned long g_to_pixel[256];    /* Converts green to pixel bits */
  99.     unsigned long b_to_pixel[256];    /* Converts blue to pixel bits */
  100.  
  101.     /* For PF_HPCR */
  102.     short hpcr_rTbl[256], hpcr_gTbl[256], hpcr_bTbl[256];
  103. };
  104.  
  105.  
  106.  
  107. /*
  108.  * Mesa wrapper for core rendering context
  109.  */
  110. struct xmesa_context {
  111.     GLcontext *gl_ctx;        /* the core library context */
  112.         XMesaVisual xm_visual;        /* Describes the buffers */
  113.     XMesaBuffer xm_buffer;        /* current framebuffer */
  114.  
  115.     Display *display;    /* == xm_visual->display */
  116.         GLboolean swapbytes;    /* Host byte order != display byte order? */
  117.  
  118.     GLuint pixelformat;        /* Current pixel format */
  119.  
  120.         GLubyte red, green, blue, alpha;/* current drawing color */
  121.     unsigned long pixel;        /* current drawing pixel value */
  122.  
  123.     GLubyte clearcolor[4];        /* current clearing color */
  124.     unsigned long clearpixel;    /* current clearing pixel value */
  125. };
  126.  
  127.  
  128.  
  129. /*
  130.  * Mesa wrapper for X window or Pixmap
  131.  */
  132. struct xmesa_buffer {
  133.     GLframebuffer *gl_buffer;/* depth, stencil, accum, etc buffers */
  134.     XMesaVisual xm_visual;    /* the X/Mesa visual */
  135.  
  136.     GLboolean pixmap_flag;    /* is the buffer a Pixmap? */
  137.     Drawable frontbuffer;    /* either a window or pixmap */
  138.     Pixmap backpixmap;    /* back buffer Pixmap */
  139.     XImage *backimage;    /* back buffer XImage */
  140.  
  141.         Drawable buffer;    /* the current buffer, either equal to */
  142.                 /* frontbuffer, backpixmap or XIMAGE (None) */
  143.  
  144.     Colormap cmap;        /* the X colormap */
  145.  
  146.     GLint db_state;        /* 0 = single buffered */
  147.                 /* BACK_PIXMAP = use Pixmap for back buffer */
  148.                 /* BACK_XIMAGE = use XImage for back buffer */
  149.  
  150.     GLuint shm;        /* X Shared Memory extension status:    */
  151.                 /*    0 = not available            */
  152.                 /*    1 = XImage support available    */
  153.                 /*    2 = Pixmap support available too    */
  154. #ifdef SHM
  155.     XShmSegmentInfo shminfo;
  156. #endif
  157.  
  158.         XImage *rowimage;    /* Used for optimized span writing */
  159.  
  160.     GLuint width, height;    /* size of buffer */
  161.  
  162.         GLint bottom;              /* used for FLIP macro below */
  163.         GLubyte *ximage_origin1;   /* used for PIXELADDR1 macro */
  164.         GLint ximage_width1;
  165.         GLushort *ximage_origin2;  /* used for PIXELADDR2 macro */
  166.         GLint ximage_width2;
  167.         GLuint *ximage_origin4;    /* used for PIXELADDR4 macro */
  168.         GLint ximage_width4;
  169.  
  170.     Pixmap stipple_pixmap;    /* For polygon stippling */
  171.     GC stipple_gc;        /* For polygon stippling */
  172.     XImage *stipple_ximage;    /* For polygon stippling */
  173.  
  174.     GC gc1;            /* GC for infrequent color changes */
  175.     GC gc2;            /* GC for frequent color changes */
  176.     GC cleargc;        /* GC for clearing the color buffer */
  177.  
  178.     /* The following are here instead of in the XMesaVisual
  179.      * because they depend on the window's colormap.
  180.      */
  181.  
  182.     /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
  183.     unsigned long color_table[576];        /* RGB -> pixel value */
  184.  
  185.     /* For PF_DITHER, PF_LOOKUP, PF_GRAYSCALE */
  186.     GLubyte pixel_to_r[65536];        /* pixel value -> red */
  187.     GLubyte pixel_to_g[65536];        /* pixel value -> green */
  188.     GLubyte pixel_to_b[65536];        /* pixel value -> blue */
  189. };
  190.  
  191.  
  192.  
  193. /* Values for xmesa->dest: */
  194. #define FRONT_PIXMAP    1
  195. #define BACK_PIXMAP    2
  196. #define BACK_XIMAGE    4
  197.  
  198.  
  199. /* Values for xmesa->pixelformat: */
  200. #define PF_INDEX    1    /* Color Index mode */
  201. #define PF_TRUECOLOR    2    /* TrueColor or DirectColor, any depth */
  202. #define PF_8A8B8G8R    3    /* 32-bit TrueColor:  8-A, 8-B, 8-G, 8-R */
  203. #define PF_8R8G8B    4    /* 32-bit TrueColor:  8-R, 8-G, 8-B */
  204. #define PF_5R6G5B    5    /* 16-bit TrueColor:  5-R, 6-G, 5-B bits */
  205. #define PF_DITHER    6    /* Dithered RGB using a lookup table */
  206. #define PF_LOOKUP    7    /* Undithered RGB using a lookup table */
  207. #define PF_HPCR        8    /* HP Color Recovery (ad@lms.be 30/08/95) */
  208. #define PF_1BIT        9    /* monochrome dithering of RGB */
  209. #define PF_GRAYSCALE    10    /* Grayscale or StaticGray */
  210.  
  211.  
  212.  
  213. /*
  214.  * If pixelformat==PF_TRUECOLOR:
  215.  */
  216. #define PACK_RGB( R, G, B )        \
  217.     (xmesa->xm_visual->r_to_pixel[R] | xmesa->xm_visual->g_to_pixel[G] | xmesa->xm_visual->b_to_pixel[B])
  218.  
  219.  
  220. /*
  221.  * If pixelformat==PF_8A8B8G8R:
  222.  */
  223. #define PACK_8A8B8G8R( R, G, B, A )    \
  224.     ( ((A) << 24) | ((B) << 16) | ((G) << 8) | (R) )
  225.  
  226.  
  227. /*
  228.  * If pixelformat==PF_8R8G8B:
  229.  */
  230. #define PACK_8R8G8B( R, G, B)     ( ((R) << 16) | ((G) << 8) | (B) )
  231.  
  232.  
  233. /*
  234.  * If pixelformat==PF_5R6G5B:
  235.  */
  236. #define PACK_5R6G5B( R, G, B)     ( ((R) << 11) | ((G) << 5) | (B) )
  237.  
  238.  
  239.  
  240. /*
  241.  * If pixelformat==PF_DITHER:
  242.  *
  243.  * Improved 8-bit RGB dithering code contributed by Bob Mercier
  244.  * (mercier@hollywood.cinenet.net).  Thanks Bob!
  245.  */
  246. #ifdef DITHER666
  247. # define _R   6
  248. # define _G   6
  249. # define _B   6
  250. # define _MIX(r,g,b)  (((r)*_G+(g))*_B+(b))
  251. #else
  252. # define _R    5
  253. # define _G    9
  254. # define _B    5
  255. # define _MIX(r,g,b)    ( ((g)<<6) | ((b)<<3) | (r) )
  256. #endif
  257. #define _DX    4
  258. #define _DY    4
  259. #define _D    (_DX*_DY)
  260.  
  261. /*#define _DITH(C,c,d)    (((unsigned)((_D*(C-1)+1)*c+d))/(_D*256))*/
  262. #define _DITH(C,c,d)    (((unsigned)((_D*(C-1)+1)*c+d)) >> 12)
  263.  
  264. #define MAXC    256
  265. static int kernel8[_DY*_DX] = {
  266.     0 * MAXC,  8 * MAXC,  2 * MAXC, 10 * MAXC,
  267.    12 * MAXC,  4 * MAXC, 14 * MAXC,  6 * MAXC,
  268.     3 * MAXC, 11 * MAXC,  1 * MAXC,  9 * MAXC,
  269.    15 * MAXC,  7 * MAXC, 13 * MAXC,  5 * MAXC,
  270. };
  271. /*static int __d;*/
  272.  
  273. /* Dither for random X,Y */
  274. #define DITHER_SETUP                        \
  275.     int __d;                        \
  276.     unsigned long *ctable = xmesa->xm_buffer->color_table;
  277.  
  278. #define DITHER( X, Y, R, G, B )                \
  279.     (__d = kernel8[(((Y)&3)<<2) | ((X)&3)],        \
  280.      ctable[_MIX(_DITH(_R, (R), __d),        \
  281.              _DITH(_G, (G), __d),        \
  282.              _DITH(_B, (B), __d))])
  283.  
  284. /* Dither for random X, fixed Y */
  285. #define XDITHER_SETUP(Y)                    \
  286.     int __d;                        \
  287.     unsigned long *ctable = xmesa->xm_buffer->color_table;    \
  288.     int *kernel = &kernel8[ ((Y)&3) << 2 ];
  289.  
  290. #define XDITHER( X, R, G, B )                \
  291.     (__d = kernel[(X)&3],                \
  292.     ctable[_MIX(_DITH(_R, (R), __d),        \
  293.             _DITH(_G, (G), __d),        \
  294.             _DITH(_B, (B), __d))])
  295.  
  296.  
  297. /*
  298.  * Dithering for flat-shaded triangles.  Precompute all 16 possible
  299.  * pixel values given the triangle's RGB color.  Contributed by Martin Shenk.
  300.  */
  301. static GLushort DitherValues[16];   /* array of (up to) 16-bit pixel values */
  302.  
  303. #define FLAT_DITHER_SETUP( R, G, B )                    \
  304.     {                                \
  305.        unsigned long *ctable = xmesa->xm_buffer->color_table;    \
  306.        int msdr = (_D*((_R)-1)+1) * (R);                \
  307.        int msdg = (_D*((_G)-1)+1) * (G);                \
  308.        int msdb = (_D*((_B)-1)+1) * (B);                \
  309.        int i;                            \
  310.        for (i=0;i<16;i++) {                        \
  311.           int k = kernel8[i];                    \
  312.           int j = _MIX( (msdr+k)>>12, (msdg+k)>>12, (msdb+k)>>12 );    \
  313.           DitherValues[i] = ctable[j];                \
  314.        }                                \
  315.         }
  316.  
  317. #define FLAT_DITHER_ROW_SETUP(Y)                    \
  318.     GLushort *ditherRow = DitherValues + ( ((Y)&3) << 2);
  319.  
  320. #define FLAT_DITHER(X)  ditherRow[(X)&3]
  321.  
  322.  
  323.  
  324. /*
  325.  * If pixelformat==PF_LOOKUP:
  326.  */
  327. #define _DITH0(C,c)    (((unsigned)((_D*(C-1)+1)*c)) >> 12)
  328.  
  329. #define LOOKUP_SETUP                        \
  330.     unsigned long *ctable = xmesa->xm_buffer->color_table
  331.  
  332. #define LOOKUP( R, G, B )            \
  333.     ctable[_MIX(_DITH0(_R, (R)),        \
  334.             _DITH0(_G, (G)),        \
  335.             _DITH0(_B, (B)))]
  336.  
  337.  
  338.  
  339. /*
  340.  * If pixelformat==PF_HPCR:
  341.  *
  342.  *      HP Color Recovery dithering               (ad@lms.be 30/08/95)
  343.  *      HP has on it's 8-bit 700-series computers, a feature called
  344.  *      'Color Recovery'.  This allows near 24-bit output (so they say).
  345.  *      It is enabled by selecting the 8-bit  TrueColor  visual AND
  346.  *      corresponding  colormap (see tkInitWindow) AND doing some special
  347.  *      dither.
  348.  */
  349. static const short HPCR_DR[2][16] = {
  350.     { 16, -4,  1,-11, 14, -6,  3, -9, 15, -5,  2,-10, 13, -7,  4, -8},
  351.     {-15,  5,  0, 12,-13,  7, -2, 10,-14,  6, -1, 11,-12,  8, -3,  9} };
  352. static const short HPCR_DG[2][16] = {
  353.     {-11, 15, -7,  3, -8, 14, -4,  2,-10, 16, -6,  4, -9, 13, -5,  1},
  354.     { 12,-14,  8, -2,  9,-13,  5, -1, 11,-15,  7, -3, 10,-12,  6,  0} };
  355. static const short HPCR_DB[2][16] = {
  356.     {  6,-18, 26,-14,  2,-22, 30,-10,  8,-16, 28,-12,  4,-20, 32, -8},
  357.     { -4, 20,-24, 16,  0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10} };
  358.  
  359. #define DITHER_HPCR( X, Y, R, G, B )                       \
  360.   ( ((xmesa->xm_visual->hpcr_rTbl[R] + HPCR_DR[(Y)&1][(X)&15]) & 0xE0)     \
  361.   |(((xmesa->xm_visual->hpcr_gTbl[G] + HPCR_DG[(Y)&1][(X)&15]) & 0xE0)>>3) \
  362.   | ((xmesa->xm_visual->hpcr_bTbl[B] + HPCR_DB[(Y)&1][(X)&15])>>6)       \
  363.   )
  364.  
  365.  
  366.  
  367. /*
  368.  * If pixelformat==PF_1BIT:
  369.  */
  370. static int const kernel1[16] = {
  371.    0*47,  9*47,  4*47, 12*47,
  372.    6*47,  2*47, 14*47,  8*47,
  373.   10*47,  1*47,  5*47, 11*47,
  374.    7*47, 13*47,  3*47, 15*47 };
  375.  
  376. #define DITHER_1BIT( X, Y, R, G, B )    \
  377.     ( ((int)(R)+(int)(G)+(int)(B)) > kernel1[(((Y)&3) << 2) | ((X)&3)] )
  378.  
  379.  
  380.  
  381. /*
  382.  * If pixelformat==PF_GRAYSCALE:
  383.  */
  384. #define GRAY_RGB( R, G, B )   xmesa->xm_buffer->color_table[(R) + (G) + (B)]
  385.  
  386.  
  387.  
  388. #define XIMAGE None
  389.  
  390.  
  391. /*
  392.  * Converts a GL window Y coord to an X window Y coord:
  393.  */
  394. #define FLIP(Y)  (xmesa->xm_buffer->bottom-(Y))
  395.  
  396.  
  397. /*
  398.  * Return the address of a 1, 2 or 4-byte pixel in the back XImage:
  399.  * X==0 is left, Y==0 is bottom.
  400.  */
  401. #define PIXELADDR1( X, Y )  \
  402.       ( xmesa->xm_buffer->ximage_origin1 - (Y) * xmesa->xm_buffer->ximage_width1 + (X) )
  403.  
  404. #define PIXELADDR2( X, Y )  \
  405.       ( xmesa->xm_buffer->ximage_origin2 - (Y) * xmesa->xm_buffer->ximage_width2 + (X) )
  406.  
  407. #define PIXELADDR4( X, Y )  \
  408.       ( xmesa->xm_buffer->ximage_origin4 - (Y) * xmesa->xm_buffer->ximage_width4 + (X) )
  409.  
  410.  
  411.  
  412. /*
  413.  * External variables:
  414.  */
  415. extern XMesaContext XMesa;
  416.  
  417.  
  418.  
  419. /*
  420.  * External functions:
  421.  */
  422.  
  423. extern unsigned long xmesa_color_to_pixel( XMesaContext xmesa,
  424.                           GLubyte r, GLubyte g, GLubyte b, GLubyte a );
  425.  
  426. extern void xmesa_alloc_back_buffer( XMesaBuffer b );
  427.  
  428. extern void xmesa_setup_DD_pointers( GLcontext *ctx );
  429.  
  430. extern points_func xmesa_get_points_func( GLcontext *ctx );
  431.  
  432. extern line_func xmesa_get_line_func( GLcontext *ctx );
  433.  
  434. extern polygon_func xmesa_get_polygon_func( GLcontext *ctx );
  435.  
  436. extern triangle_func xmesa_get_triangle_func( GLcontext *ctx );
  437.  
  438.  
  439.  
  440. #endif
  441.